/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package shoppingClient;

import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JOptionPane;

/**
 *
 * @author MinhDuc
 */
public class ShoppingClientGUI extends javax.swing.JFrame {

    /**
     * Creates new form ShoppingClientGUI
     */
    static ClientController controller;
    ShoppingClientModel model;
     HashMap<String, Product> allItem;
    
    public ShoppingClientGUI(ClientController controller) {
        allItem = new HashMap<String, Product>();
        this.controller = controller;
        model = new ShoppingClientModel();
        controller.addUser(model.getID());
        allItem = controller.load();
         this. initComponents();///here ,this is problem
        updateShowList();
        updateBuyList();
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        showListSrollPanel = new javax.swing.JScrollPane();
        showListPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        buyListScrollPanel = new javax.swing.JScrollPane();
        buyListPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        buyButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(makeTitle());
        setBackground(new java.awt.Color(0, 0, 204));

        showListSrollPanel.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        showListPanel.setBackground(new java.awt.Color(204, 204, 0));
        showListPanel.setLayout(new java.awt.GridLayout(0, 2, 5, 5));
        showListSrollPanel.setViewportView(showListPanel);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("All product");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        buyListScrollPanel.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        buyListPanel.setBackground(new java.awt.Color(0, 204, 153));
        buyListPanel.setLayout(new java.awt.GridLayout(0, 2, 5, 5));
        buyListScrollPanel.setViewportView(buyListPanel);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Shopping Cart");

        buyButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        buyButton.setText("Buy");
        buyButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buy(evt);
            }
        });
        buyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(197, 197, 197)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(showListSrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 97, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(152, 152, 152))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(buyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(91, 91, 91))))
                    .addComponent(buyListScrollPanel)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buyListScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(showListSrollPanel))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        showListSrollPanel.getAccessibleContext().setAccessibleName("showListPanel");
        buyListScrollPanel.getAccessibleContext().setAccessibleName("buyListPanel");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buyButtonActionPerformed
        // TODO add your handling code here:
        
               
       
        
    }//GEN-LAST:event_buyButtonActionPerformed

    private void buy(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buy
        
        if(controller.checkout(model.getID())){
            JOptionPane.showMessageDialog(this, "Checkout is successful!");
//        updateShowList();
//        buyListPanel.removeAll();
//        buyListPanel.repaint();
//        model.reset();
//        allItem = controller.load();
//        for(String itemName : allItem.keySet()){
//        allItem.get(itemName).reset();
//           
//            
//        }
//        
//        
//        updateShowList();
//        this.validate();
            
        }
            else{
             JOptionPane.showMessageDialog(this, "Checkout isn't successful!");  
               System.out.println("error");
                }
         updateShowList();
        buyListPanel.removeAll();
        buyListPanel.repaint();
        model.reset();
        allItem = controller.load();
        for(String itemName : allItem.keySet()){
        allItem.get(itemName).reset();
           
            
        }
        
        
        updateShowList();
        this.validate();
           
        
        
    }//GEN-LAST:event_buy

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buyButton;
    public javax.swing.JPanel buyListPanel;
    private javax.swing.JScrollPane buyListScrollPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel showListPanel;
    private javax.swing.JScrollPane showListSrollPanel;
    // End of variables declaration//GEN-END:variables

    void removeOneItem(String name) {
        model.removeItem(name);
        controller.removeProduct(model.getID(),name);
        updateBuyList();
        
    }

    void addOneItem(String name) {
        
        if(!model.contain(name)){
            model.addProduct(name, allItem.get(name));
         
        }else{
         model.addItem(name);
         
        }
        updateBuyList();
        this.validate();
        controller.addProduct(model.getID(),name);
    }

    private String makeTitle() {
        StringBuilder bf = new StringBuilder();
        bf.append("Custom ID :");
        bf.append(model.getID());
        return bf.toString();
    }
    
    private void updateShowList(){
        showListPanel.removeAll();
        showListPanel.repaint();
        for(String itemName : allItem.keySet()){
            ProductInShowlistPanel p =new ProductInShowlistPanel(allItem.get(itemName), this);
            showListPanel.add(p);
            p.setVisible(true);
           
            
        }
        
        this.validate();
    }
    
    private void updateBuyList(){
        buyListPanel.removeAll();
        buyListPanel.repaint();
        HashMap<String, Product> temp = model.getBuyList();
        for(String itemName : temp.keySet()){
            ProductInBuylistPanel p = new ProductInBuylistPanel(allItem.get(itemName), this);
            buyListPanel.add(p);
            p.setVisible(true);
           
            
        }
        
         
       
        this.validate();
    }
    
    int getID(){
        return model.getID();
    }
}
